package com.waterworld.watch.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.waterworld.watch.R;
import com.waterworld.watch.common.activity.BaseActivity;
import com.waterworld.watch.common.bean.BindWatchBean;
import com.waterworld.watch.common.bean.BindWatchUserBean;
import com.waterworld.watch.common.customview.CustomDialog;
import com.waterworld.watch.common.net.BaseObserverListener;
import com.waterworld.watch.common.net.BaseResultBean;
import com.waterworld.watch.common.util.AppManager;
import com.waterworld.watch.common.util.ScreenAdapterUtil;
import com.waterworld.watch.home.adapter.WatchOtherUserAdapter;
import com.waterworld.watch.home.interfaces.IHomeManager;
import com.waterworld.watch.home.manager.HomeManager;
import com.waterworld.watch.login.event.AutoLoginEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 编写者：Created by SunnyTang
 * 时间：2018/12/6 10:07
 * 主要作用：解除与其他用户的绑定页(活动)
 */
public class WatchUnbindOtherUserActivity extends BaseActivity {


    @BindView(R.id.header_back)
    ImageView headerBack;

    @BindView(R.id.header_title)
    TextView headerTitle;

    @BindView(R.id.header_parent)
    LinearLayout headerParent;

    @BindView(R.id.rv_other_user)
    RecyclerView rvOtherUser;

    private IHomeManager iHomeManager = HomeManager.getInstance();
    private WatchOtherUserAdapter mAdapter;
    private List<BindWatchUserBean> mList = new ArrayList<>();

    private CustomDialog mDialog;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_watch_unbind_other_user);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initView() {
        setViewSize(headerParent, ViewGroup.LayoutParams.MATCH_PARENT, ScreenAdapterUtil.getHeightPx(this) / 10);
        headerBack.setVisibility(View.VISIBLE);
        if (getIntent().getStringExtra("title").equals("transferAuthority")) {
            headerTitle.setText(getString(R.string.watch_shiftManager));
            headerTitle.setVisibility(View.VISIBLE);
        } else if (getIntent().getStringExtra("title").equals("unbindOthers")) {
            headerTitle.setText(getString(R.string.unbind_others));
            headerTitle.setVisibility(View.VISIBLE);
        }
    }

    private void initData() {
        if (iHomeManager != null) {
            iHomeManager.listBindWatchUser(new BaseObserverListener<BaseResultBean<BindWatchUserBean[]>>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(BaseResultBean<BindWatchUserBean[]> baseResultBean) {
                    for (BindWatchUserBean bean : baseResultBean.getData()) {
                        //非管理员才可以加进来
                        if (bean.getUserRole() == 1) {
                            mList.add(bean);
                        }
                    }
                    mAdapter = new WatchOtherUserAdapter(WatchUnbindOtherUserActivity.this, mList);
                    LinearLayoutManager layoutManager = new LinearLayoutManager(WatchUnbindOtherUserActivity.this);
                    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    rvOtherUser.setLayoutManager(layoutManager);
                    rvOtherUser.setAdapter(mAdapter);

                    if (getIntent().getStringExtra("title").equals("transferAuthority")) {
                        //移交管理权限
                        mAdapter.setOnClickListener(new WatchOtherUserAdapter.onClickListener() {
                            @Override
                            public void onClick(View view,int position) {
                                mDialog = new CustomDialog(WatchUnbindOtherUserActivity.this, getString(R.string.warm_prompt), String.format(getString(R.string.confirm_transfer_authority_someone),mList.get(position).getName()), new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        mDialog.dismiss();
                                        transferAuthority(mList.get(position).getUserId());
                                    }
                                }, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        mDialog.dismiss();
                                    }
                                },getString(R.string.confirm),getString(R.string.cancel));
                                mDialog.setCanotBackPress();
                                mDialog.setCanceledOnTouchOutside(false);
                                mDialog.show();
                            }
                        });
                    } else if (getIntent().getStringExtra("title").equals("unbindOthers")) {
                        //解绑其他人
                        mAdapter.setOnClickListener(new WatchOtherUserAdapter.onClickListener() {
                            @Override
                            public void onClick(View view, int position) {
                                mDialog = new CustomDialog(WatchUnbindOtherUserActivity.this, getString(R.string.warm_prompt), String.format(getString(R.string.confirm_unbind_someone),mList.get(position).getName()), new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        mDialog.dismiss();
                                        unBindWatch(mList.get(position).getUserId());
                                    }
                                }, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        mDialog.dismiss();
                                    }
                                },getString(R.string.confirm),getString(R.string.cancel));
                                mDialog.setCanotBackPress();
                                mDialog.setCanceledOnTouchOutside(false);
                                mDialog.show();
                            }
                        });
                    }
                }
            });
        }
    }
    //解绑其他人
    private void unBindWatch(Integer userId) {
        if (iHomeManager != null) {
            iHomeManager.unbindWatch(userId, new BaseObserverListener<BaseResultBean>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(BaseResultBean baseResultBean) {
                    if (baseResultBean.getCode() == 0) {
                        startActivity(new Intent(WatchUnbindOtherUserActivity.this, HomePagerActivity.class));
                        AppManager.finishActivity(WatchInfoActivity.class);
                        AppManager.finishActivity(WatchUnbindActivity.class);
                        EventBus.getDefault().post(new AutoLoginEvent(1,null));
                    }
                }
            });
        }
    }

    //移交管理权限
    private void transferAuthority(Integer authorizeeUserId) {
        if (iHomeManager != null) {
            iHomeManager.transferAuthority(authorizeeUserId, new BaseObserverListener<BaseResultBean>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(BaseResultBean baseResultBean) {
                    if (baseResultBean.getCode() == 0) {
                        startActivity(new Intent(WatchUnbindOtherUserActivity.this, HomePagerActivity.class));
                        AppManager.finishActivity(WatchInfoActivity.class);
                        AppManager.finishActivity(WatchUnbindActivity.class);
                        EventBus.getDefault().post(new AutoLoginEvent(1,null));
                    }
                }
            });
        }
    }
}
